<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="mobile" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="PicCode" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="PicUrl" :src="PicUrl" @click="getPicCode" alt="图形验证码">
        </div>
        <div class="form-item">
          <input v-model="smsCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getCode">{{ preSecond === totalSecond ? '获取验证码' : preSecond + '秒后重新获取' }}</button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>
import { getPicCode, getMsgCode, codeLogin } from '@/api/login'
export default {
  name: 'LoginPage',
  data () {
    return {
      PicKey: '', // 验证码的key，唯一标识
      PicUrl: '', // 储存的图片的地址
      totalSecond: 30,
      preSecond: 30,
      timer: null,
      PicCode: '', // 用户输入的验证码
      mobile: '15751776829',
      smsCode: ''
    }
  },
  created () {
    this.getPicCode()
  },
  methods: {
    validFn () {
      if (!/1[3-9]\d{9}/.test(this.mobile)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.PicCode)) {
        this.$toast('请输入正确的验证码')
        return false
      }
      return true
    },
    async getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.PicUrl = base64
      this.PicKey = key
      this.$toast.success('获取验证码成功')
    },
    async getCode () {
      if (!this.validFn()) {
        return
      }
      if (!this.timer && this.totalSecond === this.preSecond) {
        await getMsgCode(this.PicCode, this.PicKey, this.mobile)
        this.$toast.success('短信验证码获取成功')
        this.timer = setInterval(() => {
          this.preSecond--
          if (this.preSecond <= 0) {
            clearInterval(this.timer)
            this.preSecond = this.totalSecond
            this.timer = null
          }
        }, 1000)
      }
    },
    async login () {
      if (!this.validFn()) {
        return
      }
      if (!/^\d{6}$/.test(this.smsCode)) {
        this.$toast('请输入正确的短信验证码')
        return false
      }
      const res = await codeLogin(Number(this.mobile), Number(this.smsCode))
      this.$toast.success('登录成功')
      this.$store.commit('user/setUserInfo', {
        token: res.data.token,
        userId: res.data.userId
      })
      // 判断是否有携带查询参数
      const url = this.$route.query.backUrl || '/'
      // replace路由替换，不会新增路由历史
      this.$router.replace(url)
    }
  },
  destroyed () {
    clearInterval(this.timer)
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
